<script setup lang='ts'>
  import { h, nextTick, reactive, ref } from 'vue';
  import { luckDrawApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import serachUserid from '@/components/serachUserid/index.vue';
  import { ElMessageBox } from 'element-plus';
  const formLabelWidth = '100px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    poolList:[] as Array<any>,
  })
  interface formQuert {
    extract_pool_id:number
    pool_name:string
    user_id:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    user_id: [{ required: true, message: "请选择输入用户UUID", trigger: "blur" }],
    extract_pool_id: [{ required: true, message: "请选择抽取礼物池", trigger: "blur" }],
  }
  const handlefirstSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      const res = await luckDrawApi.wishWheelPoolUserList({
        user_id:form.user_id,
        in_pool:1
      })
      loading.close()
      const { list=[] } = res?.data || { list:[]},
      { user_id=0 } = list[0] || {user_id:0}
      user_id?ElMessageBox({
        title: '提示',
        message: h('div', { style:'padding:0 0 0 20px' }, [
          h('span', null, '该用户当前已被添加进入个人独立池是否'),
          h('span', { style: { color: 'red',} }, '移出该用户进入抽取礼物池?'),,
        ]),
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      })
      .then(async () => {
        handleSubmit(1)
      })
      .catch(() => {}):handleSubmit(1)
    })
  }
  const handleSubmit = async(if_confirm:number = 0)=>{
    if(!if_confirm) return ToastSuccess('if_confirm 不可为0')
    const loading = Toastloading()
    await luckDrawApi.wishWheelAddExtractPoolUser({
      ...form,
      if_confirm
    }).then(()=>{
      ToastSuccess('新增抽取礼物池成功')
      loading.close()
    $emit('updateList')
    closeDialog()
    }).catch(()=>{
      loading.close()
    })
  }
  const getpoolList = async()=>{
    const loading = Toastloading()
    const res = await luckDrawApi.wishWheelExtractPoolList()
    loading.close()
    const { data=[] } = res || {data:[]}
    action.poolList = data
  }
  const openDialog = async(query:any = {})=> {
    const loading = Toastloading()
    await getpoolList()
    loading.close()
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="25vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="false"
      :show-close="false"
      :close-on-press-escape="false"
      width="35%"
      title="抽取礼物池新增用户">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="用户ID" prop="user_id">
          <serachUserid v-model="form.user_id" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="抽取礼物池" prop="extract_pool_id">
            <el-select :disabled="action.disabled" v-model="form.extract_pool_id" filterable placeholder="请选择抽取礼物池">
            <el-option
              v-for="item in action.poolList"
              :label="item.pool_name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handlefirstSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
